package com.ophiux.cs.api.appraise.impl;

import java.lang.reflect.Type;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.reflect.TypeToken;
import com.ophiux.cs.api.appraise.port.IAppraiseApiPort;
import com.ophiux.cs.application.appraise.dto.AppraiseOrderRequest;
import com.ophiux.cs.application.appraise.dto.AppraiseOrderResponse;
import com.ophiux.cs.application.appraise.dto.GetCSDoctorAppraiseDetailsRequest;
import com.ophiux.cs.application.appraise.dto.GetCSDoctorAppraiseDetailsResponse;
import com.ophiux.cs.application.appraise.dto.GetCSDoctorAppraiseMasterRequest;
import com.ophiux.cs.application.appraise.dto.GetCSDoctorAppraiseMasterResponse;
import com.ophiux.cs.application.appraise.service.IAppraiseAppService;
import com.ophiux.cs.application.appraise.service.IGetCSDoctorAppraiseDetailsAppService;
import com.ophiux.cs.application.appraise.service.IGetCSDoctorAppraiseMasterAppService;
import com.ophiux.cs.application.core.AppResultCode;
import com.ophiux.cs.application.core.Request;
import com.ophiux.cs.application.core.ResponsePage;
import com.ophiux.cs.application.core.Result;
import com.ophiux.utils.parser.json.JsonUtils;
import com.ophiux.utils.parser.json.JsonUtils.TypeObtainCallback;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class AppraiseApiService implements IAppraiseApiPort{
	@Autowired
	IAppraiseAppService appraiseAppService;
	@Autowired
    IGetCSDoctorAppraiseMasterAppService getCSDoctorAppraiseMasterAppService;
    @Autowired
    IGetCSDoctorAppraiseDetailsAppService getCSDoctorAppraiseDetailsAppService;
	
	@Override
	public String appraiseOrder(String data) {
		log.info("appraiseOrder接口,请求参数："+data);
		Result<AppraiseOrderResponse> result = null;
		Request<AppraiseOrderRequest> request = null;
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				public Type getObtainType() {
					return new TypeToken<Request<AppraiseOrderRequest>>() {
					}.getType();
				}
			});
			
			result=appraiseAppService.appraiseOrder(request.getPARAM());
			
			String returnMessage = JsonUtils.toJson(result);
			log.info("appraiseOrder接口,返回参数："+returnMessage);
			return returnMessage;
		} catch (Exception e) {
			log.info("appraiseOrder接口异常：",e);
			e.printStackTrace();
			result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
			return JsonUtils.toJson(result);
		}
	}

    @Override
    public String GetCSDoctorAppraiseMaster(String data) {
        log.info("【GetCSDoctorAppraiseMaster】接口,请求参数："+data);
        Request<GetCSDoctorAppraiseMasterRequest> request = null;
        Result<List<GetCSDoctorAppraiseMasterResponse>> result = null;
        try {
            request = JsonUtils.toEntity(data, new TypeObtainCallback() {
                @Override
                public Type getObtainType() {
                    return new TypeToken<Request<GetCSDoctorAppraiseMasterRequest>>() {
                    }.getType();
                }
            });
        } catch (Exception e) {
            log.info("【GetCSDoctorAppraiseMaster】接口异常：",e);
            e.printStackTrace();
            result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
            return JsonUtils.toJson(result);
        }

        GetCSDoctorAppraiseMasterRequest dto =request.getPARAM();
        result = getCSDoctorAppraiseMasterAppService.GetCSDoctorAppraiseMaster(dto);

        String returnMessage = JsonUtils.toJson(result);
        log.info("【GetCSDoctorAppraiseMaster】接口,返回参数："+returnMessage);
        return returnMessage;
    }

    @Override
    public String GetCSDoctorAppraiseDetails(String data) {
        log.info("【getCSDoctorAppraiseDetails】接口,请求参数："+data);
        Request<GetCSDoctorAppraiseDetailsRequest> request = null;
        Result<ResponsePage<GetCSDoctorAppraiseDetailsResponse>> result = null;
        try {
            request = JsonUtils.toEntity(data, new TypeObtainCallback() {
                @Override
                public Type getObtainType() {
                    return new TypeToken<Request<GetCSDoctorAppraiseDetailsRequest>>() {
                    }.getType();
                }
            });
        } catch (Exception e) {
            log.info("【getCSDoctorAppraiseDetails】接口异常：",e);
            e.printStackTrace();
            result = Result.fail(AppResultCode.EXCEPTION, e.getMessage());
            return JsonUtils.toJson(result);
        }

        GetCSDoctorAppraiseDetailsRequest dto =request.getPARAM();
        result = getCSDoctorAppraiseDetailsAppService.GetCSDoctorAppraiseDetails(dto);

        String returnMessage = JsonUtils.toJson(result);
        log.info("【getCSDoctorAppraiseDetails】接口,返回参数："+returnMessage);
        return returnMessage;
    }
}
